import { createRouter, createWebHistory } from 'vue-router'
import Login from '../view/login.vue'
import Home from '../view/home.vue'
import MainLayout from '../view/MainLayout.vue'
import AllWenZhang from '../view/AllWenZhang.vue'
import My from '../view/My.vue'
import { useUserStore } from '../store/store'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/login',
      component: Login
    },
    {
      path: '/',
      component: MainLayout,
      children: [
        {
          path: '',
          component: Home
        },
        {
          path: '/AllWenZhang',
          component: AllWenZhang
        },
        {
          path: '/My',
          component: My
        }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  if (!userStore.getToken && to.path !== '/login') {
    next('/login')
  } else {
    next()
  }
})

export default router